﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace IBuy.Entity
{
    public abstract class Subject
    {
        // Fields
        private ArrayList observers = new ArrayList();

        // Methods
        public void Attach(Observer observer)
        {
            observers.Add(observer);
        }

        public void Detach(Observer observer)
        {
            observers.Remove(observer);
        }

        public void Notify()
        {
            foreach (Observer o in observers)
                o.Update();
        }
    }

    // "ConcreteSubject"
    public class ConcreteSubject : Subject
    {
        // Fields
        private string subjectState;

        // Properties
        public string SubjectState
        {
            get { return subjectState; }
            set { subjectState = value; }
        }
    }

    // "Observer"
    public abstract class Observer
    {
        // Methods
        abstract public void Update();
    }

    // "ConcreteObserver"
    public class ConcreteObserver : Observer
    {
        // Fields
        private string name;
        private string observerState;
        private ConcreteSubject subject;
        private StatusStrip statusStrip;
        // Constructors
        public ConcreteObserver(ConcreteSubject subject,string name,StatusStrip status)
        {
            this.subject = subject;
            this.name = name;
            statusStrip = status;
        }

        // Methods
        override public void Update()
        {
            observerState = subject.SubjectState;
            statusStrip.Items[0].Text = observerState;
        }

        // Properties
        public ConcreteSubject Subject
        {
            get { return subject; }
            set { subject = value; }
        }
    }
}
